<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Guanli as GuanliModel;
use think\Request;

class Guanli extends Controller
{
    protected $table='jc_guanli';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->guanli=new GuanliModel();
    }
    //主页显示
    public function index()
    {
        $guanli=$this->guanli->getGuanli();
        return view('index',['guanli'=>$guanli]);
    }
    //添加管理
    public function insert()
    {
        if(request()->isAjax())
        {
            $this->guanli->data([
                'gname'=>input('gname'),
                'pwd'=>md5(input('repwd')),
                'phone'=>input('phone'),
                'gid'=>input('jid')
            ]);
            $db=$this->guanli->save();
            if($db)
            {
                return 1;
            }else{
                return 2;
            }
        }else{
            $jiao=\app\admin\model\Authgroup::all();
            return view('guanliadd',['jiao'=>$jiao]);
        }
    }
    //修改
    public function update()
    {
        $id=input('id');
        if(request()->isAjax())
        {
            $id=input('id');
            $data=[
                'gname'=>input('gname'),
                'phone'=>input('phone'),
                'gid'=>input('jid')
            ];
            $where['id']=$id;
            $rs=$this->guanli->updateByWhere($where,$data);
            return $rs;
        }else{
            $rs=$this->guanli->updata();
            $jiao=\app\admin\model\Authgroup::all();
            return view('update',['rs'=>$rs,'jiao'=>$jiao]);
        }
    }
    //删除
    public function delete()
    {
        $id=input('id');
        $where['id']=$id;
        $rs=$this->guanli->deleteByWhere($where);
        if($rs)
        {
            return 1;
        }else{
            return 2;
        }
    }
    //禁用
    public function jin()
    {
        $rs=$this->guanli->updata();
        if($rs['state']==1)
        {
            $where=input('id');
            $data=['jc_guanli.state'=>0];
            $rs=$this->guanli->updateByjin($where,$data);
            return $rs;
        }else{
            $id=input('id');
            $data=['jc_guanli.state'=>1];
            $rs=$this->guanli->updateByjin($id,$data);
            return $rs;
        }
    }
}
